package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.GenerateOrderBean;
import com.wnhz.work.jy.bean.HomeFragment1.RecommendBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---填写信息
 */
public class SetOrderMsg extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "SetOrderMsg";
    private TextView title;
    private ArrayList<RecommendBean> orderList = new ArrayList<>();

    private EditText truename_ed;
    private EditText telephone_ed;
    private EditText card_no_ed;
    private EditText certificate_no_ed;
    private EditText bank_serial_ed;

    private String truename;
    private String telephone;
    private String card_no;
    private String certificate_no;
    private String bank_serial_no;

    private EditText et_content;
    private String content;
    private GenerateOrderBean generateOrderBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_order_msg);
        orderList.addAll((List<RecommendBean>) getIntent().getSerializableExtra("arrayList"));
        bank_serial_no = getIntent().getStringExtra("bank_serial_no");
        content = getIntent().getStringExtra("content");

        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("填写信息");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn).setOnClickListener(this);

        truename_ed = (EditText) findViewById(R.id.truename_ed);
        telephone_ed = (EditText) findViewById(R.id.telephone_ed);
        et_content = (EditText) findViewById(R.id.et_content);
        card_no_ed = (EditText) findViewById(R.id.card_no_ed);
        certificate_no_ed = (EditText) findViewById(R.id.certificate_no_ed);
        bank_serial_ed = (EditText) findViewById(R.id.bank_serial_ed);
    }


    /**
     * $data['order_no']=date('YmdHis').rand(1000,9999);                   //订单号
     * $data['userid']=I('userId');                                        //用户ID
     * $data['truename']=I('post.truename');                               //真实姓名
     * $data['telephone']=I('post.telephone');                             //手机号
     * $data['card_no']=I('post.card_no');                                 //银行卡号
     * $data['certificate']=I('post.certificate');                         //证件类型  1身份证 2其他
     * $data['certificate_no']=I('post.certificate_no');                   //证件号码
     * $data['message']=I('post.message');                                 //备注信息
     * $data['delivery']=I('post.delivery');                               //配送方式 0存金库 1配送 2自提（现在默认存金库）
     * $data['bank_serial']=I('post.bank_serial');                         //银行流水号
     */
    private void ewmOrder() {
        Map<String, Object> map = new HashMap<>();
        map.put("delivery", "0");
        map.put("truename", truename);
        map.put("telephone", telephone);
        map.put("card_no", card_no);
        map.put("certificate_no", certificate_no);
        map.put("bank_serial", bank_serial_no);
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        if (orderList.size() == 0) {
            return;
        }
        for (int i = 0; i < orderList.size(); i++) {
            if (orderList.get(i).getmYNum() != 0) {
                map.put(orderList.get(i).getGoodId(), orderList.get(i).getmYNum() + "");
            }
        }
        if (content != null && !"".equals(content)) {
            map.put("message", content);
        }
        showDialog();
        XUtil.Post(ConfigUtil.EWMORDER, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: " + result);
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    if ("1".equals(re)) {
                        generateOrderBean = new GenerateOrderBean();
                        generateOrderBean = gson.fromJson(result, GenerateOrderBean.class);
                       /* DecimalFormat df = new DecimalFormat("##.##");
                        String qrcode = json.optString("qrcode");
                        String guiyuanOrderid = json.optString("guiyuanOrderid");
                        String pid = json.optString("pid");*/
                        startActivity(new Intent(SetOrderMsg.this, OrderCodeActivity.class).putExtra("bean", generateOrderBean));
                    } else {
                        String info = json.optString("info");
                        Toast.makeText(getApplication(), info, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }


    private void getString() {
        truename = truename_ed.getText().toString();
        telephone = telephone_ed.getText().toString();
        card_no = card_no_ed.getText().toString();
        certificate_no = certificate_no_ed.getText().toString();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.btn:
                getString();
                if (truename == null || "".equals(truename)) {
                    MyToast("请输入客户姓名");
                } else if (!MyUtils.isMobileNO(telephone)) {
                    MyToast("请输入正确手机号");
                } else if (card_no == null || "".equals(card_no)) {
                    MyToast("请输入银行卡号");
                } else if (!MyUtils.isIDCard(certificate_no)) {
                    MyToast("请输入正确身份证号");
                } else {
                    ewmOrder();
                }
                /*if (bank_serial_no == null || "".equals(bank_serial_no)){
                    MyToast("请输入银行流水号");
                }else*/
                break;
        }
    }
}
